package com.example.goandroiddemo.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.test.AndroidTestCase;

import com.chouren.goandroid.http.client.ApacheHttpClient;
import com.chouren.goandroid.http.client.HttpClient;
import com.chouren.goandroid.http.client.HttpMethod;
import com.chouren.goandroid.http.parser.FileParser;
import com.chouren.goandroid.http.request.Request;
import com.chouren.goandroid.http.response.Response;
import com.chouren.goandroid.util.FileUtil;
import com.chouren.goandroid.util.Logger;

public class HttpTest extends AndroidTestCase {
	HttpClient httpClient = ApacheHttpClient.getInstance(getContext(), 10000, false);
	
	public void testDefaultHttp() {
		Request request = new Request("http://baidu.com");
		Response response = httpClient.execute(request);
		System.out.println("-->" + response.getString());
	}
	
	public void testDownloadFileHttp() {
		String imageUrl = "http://pic.yesky.com/imagelist/07/37/5146451_2754_1000x500.jpg";
		Logger.debug(null, FileUtil.getSDCardRoot().getAbsolutePath());
		File file = httpClient.execute(imageUrl, new FileParser(FileUtil.getSDCardRoot().getAbsolutePath() + "/lite1.jpg"), HttpMethod.Get);
		System.out.println("-->" + file.getAbsolutePath() + ";" + file.length());
	}
	
	public void testUploadFileRequest() {
		try {
			FileInputStream fis = new FileInputStream(new File(FileUtil.getSDCardRoot().getAbsolutePath() + "/lite1.jpg"));
			String url = "http://192.168.2.108:8080/LiteHttpServer/ReceiveFile";
			Request request = new Request(url);
			request.setMethod(HttpMethod.Post)
				   .addParam("file1", new File(FileUtil.getSDCardRoot().getAbsolutePath() + "/lite2.jpg"), "image/jpeg")
				   .addParam("file2", new File(FileUtil.getSDCardRoot().getAbsolutePath() + "/lite.jpg"), "application/octet-stream")
				   .addParam("file3", fis, "lite.jpg", "image/jpeg");
			Response res = httpClient.execute(request);
			System.out.println("-->" + res.getString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
